package com.tiantian.homework_6_mvc.controller;


import com.tiantian.homework_6_mvc.pojo.Result;
import com.tiantian.homework_6_mvc.pojo.User;
import com.tiantian.homework_6_mvc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/all")
    public Result getAll(@RequestParam(value = "username", required = false) String username,
                         @RequestParam(value = "gender", required = false) String gender,
                         @RequestParam(value = "uid", required = false) Integer uid,
                         @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                         @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return userService.getAll(uid, username, gender, currentPage, pageSize);
    }

    @GetMapping("/{uid}")
    public Result getUser(@PathVariable Integer uid){
        return userService.getUserByUid(uid);
    }

    @DeleteMapping("/delete/{uid}")
    public Result deleteUser(@PathVariable Integer uid) {
        return userService.deleteUser(uid);
    }

    @PostMapping("/add")
    public Result addUser(@RequestBody User user){
        return userService.addUser(user);
    }

    @PutMapping("/update")
    public Result update(@RequestBody User user){
        return userService.update(user);
    }
}
